package com.singleton;

/**
 * @Author: LiQingBo
 * @Date: 2021/6/19-06-19-23:24
 * @Description: com.singleton
 * @version: 1.0
 * 懒汉式
 * 虽然达到了按需初始化的目的，但是带来线程不安全的问题(多线程访问有影响)
 */
public class Mgr03 {
    private static Mgr03 INSTANCE;

    private Mgr03() {
    }

    public static Mgr03 getInstance() {
        if (INSTANCE == null) {
            try {
                Thread.sleep(1);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            INSTANCE = new Mgr03();
        }
        return INSTANCE;
    }

    public void m() {
        System.out.println("m");
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.println(Mgr03.getInstance().hashCode());
            }).start();
        }
    }
}
